<?php
class Users extends Model{
	
	function getAll($pos=''){
		$sql ="SELECT *,u.id,u.enabled,u.sales FROM users u ";
		$sql.="LEFT JOIN user_group g ON g.id=u.group_id ";
		$sql.="LEFT JOIN user_position p ON p.id=u.pos_id ";
        $sql.="LEFT JOIN user_division d ON d.div_id=u.division_id ";
        if($pos<>""):
            $sql.=" ORDER BY p.pos_name ASC";
        endif;
		$query=$this->db->query($sql);
		return $query->result_array();
	}
    
    function getLastActivity($id){
        $sql =" SELECT *,at.id FROM activity_transaction at ";
        $sql.=" LEFT JOIN activity a ON a.id=at.activity_id ";
        $sql.=" LEFT JOIN customers c ON c.id=at.customer_id ";
        $sql.=" WHERE at.pic='".$id."'";
		$sql.=" ORDER BY at.activity_date DESC ";
		$query=$this->db->query($sql);
		return $query->result_array();
    }
    
    function getLastJob($id){
        $sql ="SELECT *,j.id,((p.invest + (p.invest * j.ppn / 100) - j.discount) ) as total FROM jobs j ";
		$sql.="LEFT JOIN products p ON p.id=j.product_id ";
		$sql.="LEFT JOIN customers c ON c.id=j.customer_id ";
        $sql.=" WHERE j.pic='".$id."'";
        $query=$this->db->query($sql);
		return $query->result_array();
    }
    
    function getCek(){
        $sql ="SELECT * FROM users u ";
		$sql.="WHERE user_no = '".$this->input->post('no')."' LIMIT 1";
        $query=$this->db->query($sql);
		return $query->row_array();
    }
    
    function getData($id){
		$sql ="SELECT *,u.id,u.enabled FROM users u ";
		$sql.="LEFT JOIN user_group g ON g.id=u.group_id ";
		$sql.="LEFT JOIN user_position p ON p.id=u.pos_id ";
        $sql.="LEFT JOIN user_division d ON d.div_id=u.division_id ";
        $sql.="WHERE user_no = '".$id."'  ";
        $sql.="OR username = '".$id."' LIMIT 1";
		$query=$this->db->query($sql);
		return $query->row_array();
	}
    
    function getSave(){
        $value = array  (
                            'user_no'   =>  trim($this->input->post('no')),
                            'group_id'  =>  $this->input->post('group'),
                            'pos_id'    =>  $this->input->post('pos'),
                            'division_id'=> $this->input->post('division'),
                            'enabled'   =>  trim($this->input->post('enabled')),
                            'sales'     =>  $this->input->post('sales'),
                            'name'      =>  $this->input->post('name'),
                            'username'  =>  $this->input->post('user_call'),
                            'birthday'  =>  $this->input->post('birthday'),
                            'status'    =>  $this->input->post('status'),
                            'gender'    =>  $this->input->post('gender'),
                            'password'  =>  SHA1($this->input->post('password')),
                            'address'   =>  $this->input->post('address'),
                            'email'     =>  trim($this->input->post('email')),
                            'tlp'       =>  $this->input->post('tlp'),
                            'city'      =>  $this->input->post('city'),
                            'zip_code'  =>  $this->input->post('zipcode'),
                            'hobby'     =>  $this->input->post('hobby'),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->insert('users',$value);                
    }
    
    function getUpdate(){
        $value = array  (
                            'group_id'  =>  $this->input->post('group'),
                            'pos_id'    =>  $this->input->post('pos'),
                            'division_id'=> $this->input->post('division'),
                            'name'      =>  $this->input->post('name'),
                            'enabled'   =>  trim($this->input->post('enabled')),
                            'sales'     =>  $this->input->post('sales'),
                            'username'  =>  $this->input->post('user_call'),
                            'birthday'  =>  $this->input->post('birthday'),
                            'status'    =>  $this->input->post('status'),
                            'gender'    =>  $this->input->post('gender'),
                            'address'   =>  $this->input->post('address'),
                            'tlp'       =>  $this->input->post('tlp'),
                            'email'     =>  trim($this->input->post('email')),
                            'city'      =>  $this->input->post('city'),
                            'zip_code'  =>  $this->input->post('zipcode'),
                            'hobby'     =>  $this->input->post('hobby'),
                            'user_stamp'=>  $this->session->userdata('user_id'),
                            'date_stamp'=>  date('Y-m-d')
                        );
        $this->db->where('user_no',$this->input->post('id'));                 
        $this->db->update('users',$value);                
    }
    
    function getRemove($id){
        $this->db->where('user_no',$id);
        $this->db->delete('users');
    }
}